﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geometry;
using ESRI.ArcGIS.Controls;
using ESRI.ArcGIS.Display;
using ESRI.ArcGIS.Geodatabase;
using ESRI.ArcGIS.SystemUI;
using ESRI.ArcGIS.esriSystem;


namespace MyGIS.Forms
{
    public partial class DrawStationsForm : Form
    {
        private AxMapControl mapControl;

        public DrawStationsForm(AxMapControl control)
        {
            InitializeComponent();
            mapControl = control;
        }

        private void DrawStationsForm_Load(object sender, EventArgs e)
        {
            cmbLayers.Items.Clear();

            for (int i = 0; i < mapControl.Map.LayerCount; i++)
            {
                ILayer layer = mapControl.Map.get_Layer(i);
                if (layer is IFeatureLayer)
                    cmbLayers.Items.Add(layer.Name);
            }

            if (cmbLayers.Items.Count > 0)
            {
                cmbLayers.SelectedIndex = 0;
                // 手动触发一次字段列表刷新
                cmbLayers_SelectedIndexChanged(null, null);
            }
        }


        private void cmbLayers_SelectedIndexChanged(object sender, EventArgs e)
        {
            cmbFields.Items.Clear();
            string selectedLayerName = cmbLayers.SelectedItem?.ToString();
            if (string.IsNullOrEmpty(selectedLayerName)) return;

            for (int i = 0; i < mapControl.Map.LayerCount; i++)
            {
                ILayer layer = mapControl.Map.get_Layer(i);
                if (layer.Name == selectedLayerName && layer is IFeatureLayer fl)
                {
                    IFields fields = fl.FeatureClass.Fields;
                    for (int j = 0; j < fields.FieldCount; j++)
                    {
                        IField field = fields.get_Field(j);
                        if (field.Type == esriFieldType.esriFieldTypeDouble ||
                            field.Type == esriFieldType.esriFieldTypeInteger)
                        {
                            cmbFields.Items.Add(field.Name);
                        }
                    }
                }
            }

            if (cmbFields.Items.Count > 0)
                cmbFields.SelectedIndex = 0;
        }

        private void btnDraw_Click(object sender, EventArgs e)
        {
            string selectedLayerName = cmbLayers.SelectedItem?.ToString();
            string selectedField = cmbFields.SelectedItem?.ToString();

            if (string.IsNullOrEmpty(selectedLayerName) || string.IsNullOrEmpty(selectedField))
            {
                MessageBox.Show("请先选择图层和字段");
                return;
            }

            IFeatureLayer featureLayer = null;
            for (int i = 0; i < mapControl.Map.LayerCount; i++)
            {
                ILayer layer = mapControl.Map.get_Layer(i);
                if (layer.Name == selectedLayerName && layer is IFeatureLayer)
                {
                    featureLayer = (IFeatureLayer)layer;
                    break;
                }
            }

            if (featureLayer == null)
            {
                MessageBox.Show("未找到图层");
                return;
            }

            DrawStationsFromLayer(featureLayer, selectedField);
            this.Close();
        }

        private void DrawStationsFromLayer(IFeatureLayer featureLayer, string fieldName)
        {
            IGraphicsContainer graphicsContainer = mapControl.ActiveView.GraphicsContainer;
            graphicsContainer.DeleteAllElements();

            IFeatureCursor cursor = featureLayer.FeatureClass.Search(null, false);
            IFeature feature;
            while ((feature = cursor.NextFeature()) != null)
            {
                int fieldIndex = feature.Fields.FindField(fieldName);
                if (fieldIndex < 0) continue;

                object val = feature.get_Value(fieldIndex);
                if (val == null || val == DBNull.Value) continue;

                double value = Convert.ToDouble(val);
                IColor color = SymbolHelper.GetColorByPollutant(fieldName, value);
                ISimpleMarkerSymbol symbol = SymbolHelper.CreatePollutionSymbol(8, color);

                IPoint point = feature.ShapeCopy as IPoint;
                IMarkerElement markerElement = new MarkerElementClass { Symbol = symbol };
                ((IElement)markerElement).Geometry = point;
                graphicsContainer.AddElement((IElement)markerElement, 0);
            }

            mapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGraphics, null, null);
        }

       

    }
}
